package com.awccis.admin.service.impl;

import com.awccis.admin.dao.AdminOrderDao;
import com.awccis.admin.model.config.ConfigBean;
import com.awccis.admin.model.order.BreakOrderBean;
import com.awccis.admin.model.order.OrderSimpleInfo;
import com.awccis.admin.service.AdminOrderService;
import com.awccis.common.pool.DriverPool;
import com.awccis.common.session.DriverSession;
import com.awccis.customer.model.order.OrderBean;
import com.awccis.driver.model.DriverInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author: txp
 * @date: 2018/5/19 on 下午9:02
 * @since: 1.0
 * @description:
 **/
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Resource
    AdminOrderDao adminOrderDao;

    /**
     * 获取所有订单的简单信息
     *
     * @param hashMap startCount，limitCounts
     * @return 订单列表
     */
    @Override
    public List<OrderSimpleInfo> getAllSimpleOrderInfo(HashMap hashMap) {
        return adminOrderDao.selectSimpleOrderInfo(hashMap);
    }

    /**
     * 获取订单总数
     *
     * @return 订单总数
     */
    @Override
    public Integer getOrdersSumNumber() {
        return adminOrderDao.selectCountOrders();
    }

    /**
     * 通过司机的driverID获取司机的订单
     *
     * @param hashMap driverId , startCount, limitCounts
     * @return 订单列表
     */
    @Override
    public List<OrderSimpleInfo> getDriverOrderById(HashMap hashMap) {
        return adminOrderDao.selectSimpleOrderInfoByDriverId(hashMap);
    }

    /**
     * 通过乘客获取司机的订单
     *
     * @param hashMap customerPhone , startCount, limitCounts
     * @return 订单列表
     */
    @Override
    public List<OrderSimpleInfo> getCustomerOrderByPhone(HashMap hashMap) {
        return adminOrderDao.selectSimpleOrderInfoByCustomerPhone(hashMap);
    }

    /**
     * 通过订单ID获取订单详情
     *
     * @param orderId orderId
     * @return 订单信息
     */
    @Override
    public OrderBean getOrderDetailInfoById(int orderId) {
        return adminOrderDao.selectDetailOrderInfoByOrderId(orderId);
    }

    /**
     * 获取所有未完成订单
     *
     * @return list
     */
    @Override
    public List<OrderBean> getAllNoFinishOrder() {
        return adminOrderDao.selectAllNoFinishOrder();
    }

    /**
     * 获取当前driver的未完成订单
     *
     * @param driverId driverId
     * @return order
     */
    @Override
    public OrderBean getBreakOrder(int driverId) {
        return adminOrderDao.selectBreakOrderByDriverId(driverId);
    }

    /**
     * 查找可以推送的司机
     *
     * @return list
     */
    @Override
    public List<DriverSession> getDriverToSend() {
        return DriverPool.getSimpleAllOnlineDriver();
    }

    /**
     * 转移订单
     *
     * @param oldOrderId     原订单ID
     * @param newOrder       新订单数据
     * @param breakOrderBean 故障订单信息
     * @return 执行结果
     */
    @Override
    public Boolean transferOrder(int oldOrderId, OrderBean newOrder, BreakOrderBean breakOrderBean) {
        boolean updatePid = adminOrderDao.updateOrderParentId(oldOrderId) == 1;
        boolean insertBreakOrder = adminOrderDao.insertBreakOrder(breakOrderBean) == 1;
        boolean insertNewOrder = adminOrderDao.insertNewOder(newOrder) == 1;
        System.out.println("更改parentID：" + updatePid + "\n 添加故障信息：" + insertBreakOrder + "\n 添加新订单：" + insertNewOrder);
        return updatePid && insertBreakOrder && insertNewOrder;
    }

    /**
     * 获取所有系统参数
     *
     * @return 系统参数
     */
    @Override
    public ConfigBean getSystemInfo() {
        return adminOrderDao.selectSystemConfigInfo();
    }

    /**
     * 更改系统参数
     *
     * @param configBean 系统参数
     * @return 更改结果
     */
    @Override
    public boolean setSystemInfo(ConfigBean configBean) {
        return adminOrderDao.updateSystemConfigInfo(configBean) == 1;
    }

    /**
     * 获取该司机订单总数
     *
     * @param hashMap 司机信息
     * @return 订单总数
     */
    @Override
    public Integer getOrdersSumNumByDId(HashMap hashMap) {
        return adminOrderDao.selectCountOrdersByDID(hashMap);
    }

    /**
     * 获取该乘客订单总数
     *
     * @param hashMap 乘客信息
     * @return 订单总数
     */
    @Override
    public Integer getOrdersSumNumByCId(HashMap hashMap) {
        return adminOrderDao.selectCountOrdersByCID(hashMap);
    }
}
